{extend name="layouts/app" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">
            <span>个人中心</span>
        </div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="required">*</span>用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input username" name="username" value="{{$adminInfo.username}}" data-username="{{$adminInfo.username}}" maxlength="12" lay-verify="required" placeholder="" autocomplete="off" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="required">*</span>登录邮箱</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="email" value="{{$adminInfo.email}}" maxlength="12" lay-verify="required" placeholder="" autocomplete="off" >
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="required">*</span>头像</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <div id="upload-file" class="upload_file-box">
                            <input type="hidden" class="layui-input avatar" name="avatar" value="{{$adminInfo.avatar}}" >
                            <span id="upload-btn" class="upload-btn" data-multi="false" data-field="avatar" data-total_count="1">
                                <img class="load_img" src="{{$adminInfo.full_avatar}}" alt="上传头像"/>
                            </span>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn layui-btn-primary close-btn">取消</button>
                            <button type="button" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="laySave">保存 <i class="layui-icon layui-icon-release"></i></button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<input type="hidden" class="save_url" value="{{:url('/admin/infoPost')}}">
<input type="hidden" class="check_username_url" value="{{:url('/admin/checkLoginName')}}">
{/block}

{block name="pagescript"}
<script src="{{:asset('__STATIC__/admin/pages/js/upload_file.js')}}" type="text/javascript"></script>
<script type="text/javascript">
    layui.use(['form', 'layer'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery;

        var check_username_url = $('.check_username_url').val();
        var save_url = $('.save_url').val();


        // 表单提交
        form.on("submit(laySave)", function (data) {
            var username = $('.username').val();
            var ori_username = $('.username').attr('data-username');
            if (username != ori_username) {
                var url = check_username_url;
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: {'username': username},
                    dataType: 'json',
                    success: function (res) {
                        if (res.status == 1) {
                            layer.msg('该用户名已经存在！', {icon: 5});
                            $('.username').focus();
                            return false;
                        } else {
                            submitForm(data);
                        }
                    }
                });
                return false;
            }

            submitForm(data);
        });

        // 表单提交
        function submitForm(data) {
            // 弹出loading
            var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});

            $.ajax({
                url: save_url,
                data: data.field,
                type: "post",
                dataType: "json",
                success: function (res) {
                    top.layer.close(index);
                    top.layer.msg(res.msg);
                    if (res.status == 1) {
                        layer.closeAll("iframe");
                        // 刷新父页面
                        parent.location.reload();
                    }
                },
                error: function (data) {
                    layer.msg("服务器无响应");
                }
            });
        }
    });
</script>
{/block}